package com.klm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.klm.dao.TranslatePlatformConfigDao;
import com.klm.entity.TranslatePlatformConfigEntity;
import com.klm.service.TranslatePlatformConfigService;
import com.klm.service.TranslatePlatformCacheService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TranslatePlatformConfigServiceImpl implements TranslatePlatformConfigService {

    private final TranslatePlatformConfigDao configDao;
    private final TranslatePlatformCacheService cacheService;

    @Override
    public TranslatePlatformConfigEntity getById(String id) {
        // 先从缓存获取
        TranslatePlatformConfigEntity cachedConfig = cacheService.getCachedConfig(id);
        if (cachedConfig != null) {
            return cachedConfig;
        }
        // 缓存未命中，从数据库获取
        return configDao.selectById(id);
    }

    @Override
    public List<TranslatePlatformConfigEntity> listAll() {
        LambdaQueryWrapper<TranslatePlatformConfigEntity> wrapper = new LambdaQueryWrapper<>();
        return configDao.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(TranslatePlatformConfigEntity config) {
        configDao.insert(config);
        // 更新缓存
        cacheService.cachePlatformConfig(config);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TranslatePlatformConfigEntity config) {
        configDao.updateById(config);
        // 更新缓存
        cacheService.cachePlatformConfig(config);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        configDao.deleteById(id);
        // 删除缓存
        cacheService.removeCachedConfig(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void enablePlatform(String platformCode) {
        LambdaUpdateWrapper<TranslatePlatformConfigEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TranslatePlatformConfigEntity::getPlatformCode, platformCode)
                .set(TranslatePlatformConfigEntity::getStatus, 1);
        configDao.update(null, wrapper);
        // 更新缓存
        cacheService.removeCachedConfig(platformCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disablePlatform(String platformCode) {
        LambdaUpdateWrapper<TranslatePlatformConfigEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TranslatePlatformConfigEntity::getPlatformCode, platformCode)
                .set(TranslatePlatformConfigEntity::getStatus, 0);
        configDao.update(null, wrapper);
        // 更新缓存
        cacheService.removeCachedConfig(platformCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByteThreshold(String platformCode, Long newThreshold) {
        LambdaUpdateWrapper<TranslatePlatformConfigEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TranslatePlatformConfigEntity::getPlatformCode, platformCode)
                .set(TranslatePlatformConfigEntity::getByteThreshold, newThreshold);
        configDao.update(null, wrapper);
        // 更新缓存
        cacheService.removeCachedConfig(platformCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateQpsLimit(String platformCode, Integer newQpsLimit) {
        LambdaUpdateWrapper<TranslatePlatformConfigEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TranslatePlatformConfigEntity::getPlatformCode, platformCode)
                .set(TranslatePlatformConfigEntity::getQpsLimit, newQpsLimit);
        configDao.update(null, wrapper);
        // 更新缓存
        cacheService.removeCachedConfig(platformCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePriority(String platformCode, Integer newPriority) {
        LambdaUpdateWrapper<TranslatePlatformConfigEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TranslatePlatformConfigEntity::getPlatformCode, platformCode)
                .set(TranslatePlatformConfigEntity::getPriority, newPriority);
        configDao.update(null, wrapper);
        // 更新缓存
        cacheService.removeCachedConfig(platformCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateResetRule(String platformCode, Integer resetType, Integer resetDay) {
        LambdaUpdateWrapper<TranslatePlatformConfigEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TranslatePlatformConfigEntity::getPlatformCode, platformCode)
                .set(TranslatePlatformConfigEntity::getResetType, resetType)
                .set(TranslatePlatformConfigEntity::getResetDay, resetDay);
        configDao.update(null, wrapper);
        // 更新缓存
        cacheService.removeCachedConfig(platformCode);
    }

    @Override
    public boolean isPlatformAvailable(String platformCode) {
        // TODO: 需要实现平台可用性检查逻辑，包括状态、QPS、字节数阈值等检查
        return false;
    }

    @Override
    public List<String> getAvailablePlatforms() {
        LambdaQueryWrapper<TranslatePlatformConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TranslatePlatformConfigEntity::getStatus, 1);
        return configDao.selectList(wrapper).stream()
                .map(TranslatePlatformConfigEntity::getPlatformCode)
                .collect(Collectors.toList());
    }

    @Override
    public String selectBestPlatform() {
        // TODO: 需要实现根据优先级和可用性选择最佳平台的逻辑
        return null;
    }
}
